package com.itxx.keyCase;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/ImgServlet")
public class ImgServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建输入流 指定项目的图片
        ServletContext servletContext = getServletContext();
        String realPath = servletContext.getRealPath("/img/AAA.jpg");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(realPath));
        //获取输出流 通过response对象获取
        ServletOutputStream sos = response.getOutputStream();
        //读写数据
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            sos.write(bytes, 0, len);
            sos.flush();
        }
        //释放资源
        bis.close();

    }
}
